﻿using System.Collections.Generic;
using System.Xml;

namespace GeoSketch2.Archaeo
{
    /// <summary>
    /// A survey logbook, used for the levelling of the site
    /// </summary>
    public class SurveyLogBook
    {
        #region XMLElementsName
#pragma warning disable 1591
        public const string XML_Records = "Records";
#pragma warning restore 1591
        #endregion

        #region Fields
        /// <summary>
        /// The records of the logbook
        /// </summary>
        public List<SurveyLogBookRecord> Records;

        /// <summary>
        /// The excavation unit of the logbook
        /// </summary>
        public ExcavationArea Area; 
        #endregion

        #region Constructor
        /// <summary>
        /// A survey logbook, used for the levelling of the site
        /// </summary>
        /// <param name="Unit">The excavation unit of the logbook</param>
        public SurveyLogBook(ExcavationArea Area)
            : this(Area, new List<SurveyLogBookRecord>())
        {
        }

        /// <summary>
        /// A survey logbook, used for the levelling of the site
        /// </summary>
        /// <param name="Unit">The excavation unit of the logbook</param>
        /// <param name="Records">The records of the logbook</param>
        public SurveyLogBook(ExcavationArea Area, List<SurveyLogBookRecord> Records)
        {
            this.Area = Area;
            this.Records = Records;
        }

        /// <summary>
        /// A survey logbook, used for the levelling of the site
        /// </summary>
        /// <param name="Xr">The XmlReader to read from</param>
        /// <param name="Site">The site of the logbook</param>
        public SurveyLogBook(XmlReader Xr, ArchaeoProject Project)
            : this(null)
        {
            Xr.ReadToFollowing(typeof(ExcavationArea).FullName);
            Project.GetExcavationAreaFromName(Xr.ReadString()).SurveyLogBook = this;

            Xr.ReadToFollowing(XML_Records);
            if (!Xr.IsEmptyElement)
            {
                Xr.ReadToFollowing(typeof(SurveyLogBookRecord).FullName);
                while (!(Xr.NodeType == XmlNodeType.EndElement && Xr.Name == XML_Records))
                {
                    this.Records.Add(new SurveyLogBookRecord(Xr, Project));
                    Xr.ReadEndElement();
                }
                Xr.ReadEndElement();
            }
            else
            {
                Xr.Read();
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Gets the record of a benchmark
        /// </summary>
        public SurveyLogBookRecord GetBenchmarkRecord(Benchmark Benchmark)
        {
            foreach (SurveyLogBookRecord Record in Records)
            {
                if (Record.CurrentBenchmark == Benchmark)
                {
                    return Record;
                }
            }
            return null;
        }

        /// <summary>
        /// Writes the object to a file
        /// </summary>
        /// <param name="Xw">The XmlWriter to write to</param>
        public void ToFile(XmlWriter Xw)
        {
            Xw.WriteStartElement(this.GetType().FullName);

            Xw.WriteStartElement(typeof(ExcavationArea).FullName);
            Xw.WriteString(this.Area.Name);
            Xw.WriteEndElement();

            #region Records
            Xw.WriteStartElement(XML_Records);
            foreach (SurveyLogBookRecord r in Records)
            {
                r.ToFile(Xw);
            }
            Xw.WriteEndElement();
            #endregion

            Xw.WriteEndElement();
        } 
        #endregion
    }
}
